/*
 * @(#)MessageResourcesFactory.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

import java.util.Locale;

import org.apache.struts.util.MessageResources;

/**
  MessageResourcesFactory.java

  This class is the singleton to store the message resources used in the system.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   07/07/2003
    @version         $Revision: 1.5.26.1 $
*/

public class MessageResourcesFactory {

  public static final String REVISION = "$Revision: 1.5.26.1 $";

  protected static MessageResources applicationResources;
  protected static MessageResources customizeResources;

  private MessageResourcesFactory() {
  }

  public static void init(MessageResources mr, MessageResources cr) {
    applicationResources = mr;
    customizeResources = cr;
  }

  /**
   * getMessage
   *
   * @param loc    The locale of the message.
   * @param key    The message key.
   * @param arg1   The argument one for that message.
   * @param arg2   The argument two for that message.
   * @return       The string representation of the message.
   */
  public static String getMessage(Locale loc, String key, String arg1, String arg2) {
    if (key.startsWith("customize")) {
      if (arg2==null) {
        if (arg1 == null) {
          return(customizeResources.getMessage(loc, key));
        } else {
          return(customizeResources.getMessage(loc, key, arg1));
        }
      } else {
        return(customizeResources.getMessage(loc, key, arg1, arg2));
      }
    } else {
      if (arg2==null) {
        if (arg1 == null) {
          return(applicationResources.getMessage(loc, key));
        } else {
          return(applicationResources.getMessage(loc, key, arg1));
        }
      } else {
        return(applicationResources.getMessage(loc, key, arg1, arg2));
      }
    }
  }
  
  /**
   * getMessage
   *
   * @param loc    The locale of the message.
   * @param key    The message key.
   * @param arg1   The argument one for that message.
   * @param arg2   The argument two for that message.
   * @param arg3   The argument two for that message.
   * @return       The string representation of the message.
   */
  public static String getMessage(Locale loc, String key, String arg1, String arg2, String arg3) {
    if (key.startsWith("customize")) {
      if (arg3 == null) {
        if (arg2==null) {
            if (arg1 == null) {
                return(customizeResources.getMessage(loc, key));
            } else {
                return(customizeResources.getMessage(loc, key, arg1));
            }
        } else {
            return(customizeResources.getMessage(loc, key, arg1, arg2));
        }
      } else {
        return(customizeResources.getMessage(loc, key, arg1, arg2, arg3));
      }
    } else {
      if (arg3 == null) {
        if (arg2==null) {
            if (arg1 == null) {
                return(applicationResources.getMessage(loc, key));
            } else {
                return(applicationResources.getMessage(loc, key, arg1));
            }
        } else {
            return(applicationResources.getMessage(loc, key, arg1, arg2));
        }
      } else {
        return(applicationResources.getMessage(loc, key, arg1, arg2, arg3));
      }
    }
  }

  /**
   * getMessage
   *
   * @param loc    The locale of the message.
   * @param key    The message key.
   * @param args   The argument array for that message.
   * @return       The string representation of the message.
   */
  public static String getMessage(Locale loc, String key, Object args[]) {
    if (key.startsWith("customize")) {
        return(customizeResources.getMessage(loc, key, args));
    } else {
        return(applicationResources.getMessage(loc, key, args));
    }
  }

  /**
   * getMessage
   *
   * @param loc    The locale of the message.
   * @param key    The message key.
   * @param arg1   The argument one for that message.
   * @return       The string representation of the message.
   */
  public static String getMessage(Locale loc, String key, String arg1) {
    return(getMessage(loc, key, arg1, null));
  }

  /**
   * getMessage
   *
   * @param loc    The locale of the message.
   * @param key    The message key.
   * @return       The string representation of the message.
   */
  public static String getMessage(Locale loc, String key) {
    return(getMessage(loc, key, null, null));
  }

}